package com.gdl.yygh.hosp.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gdl.yygh.common.exception.HospitalException;
import com.gdl.yygh.common.result.Result;
import com.gdl.yygh.common.result.ResultCodeEnum;
import com.gdl.yygh.hosp.service.HospitalSetService;
import com.gdl.yygh.hospital.model.hosp.HospitalSet;
import com.gdl.yygh.hospital.vo.hosp.HospitalSetQueryVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.*;


@RestController
@RequestMapping("/admin/hosp/hospital")
//@CrossOrigin
public class HospitalController {
    @Autowired
    private HospitalSetService hospitalSetService;


    @GetMapping("findAll")
    public Result findAllHospitalSet() {
        List<HospitalSet> list = hospitalSetService.list();
        return Result.ok(list);
    }

    //分页查询逻辑
    @PostMapping("findPage/{current}/{limit}")
    public Result findAllHospitalSet(@PathVariable Long current,
                                     @PathVariable Long limit,
                                     @RequestBody(required = false) HospitalSetQueryVo hospitalSetQueryVo) {
        Page<HospitalSet> page = new Page<>(current, limit);
        QueryWrapper<HospitalSet> wrapper = new QueryWrapper<>();
        if (hospitalSetQueryVo != null) {
            String hosname = hospitalSetQueryVo.getHosname();
            if (!StringUtils.isEmpty(hosname)) {
                wrapper.like("hosname", hosname);
            }

            String hoscode = hospitalSetQueryVo.getHoscode();
            if (!StringUtils.isEmpty(hoscode)) {
                wrapper.like("hoscode", hoscode);
            }
        }
        Page<HospitalSet> pageHospitalSet = hospitalSetService.page(page, wrapper);
        return Result.ok(pageHospitalSet);
    }

    @GetMapping("getHospitalSet/{id}")
    public Result getHospitalSet(@PathVariable Long id) {
        HospitalSet vo = hospitalSetService.getById(id);
        return Result.ok(vo);
    }

    @PostMapping("add")
    public Result addHospitalSet(@RequestBody(required = true) HospitalSet hospitalSet) {
        //1可食用
        hospitalSet.setStatus(1);

        String sign = System.currentTimeMillis() + "" + new Random().nextInt(10000);
        System.out.println(sign + "=====================================");
        hospitalSet.setSignKey(sign);

        boolean flag = hospitalSetService.save(hospitalSet);
        return result(flag);
    }

    @DeleteMapping("{id}")
    public Result removeById(@PathVariable Long id) {
        boolean flag = hospitalSetService.removeById(id);
        return result(flag);
    }

    @DeleteMapping("batchRemove")
    public Result batchRemove(@RequestBody(required = true) List<Long> list) {
        boolean flag = hospitalSetService.removeByIds(list);
        return result(flag);
    }

    @PostMapping("updateHospitalSet")
    public Result getHospitalSet(@RequestBody(required = true) HospitalSet hospitalSet) {

        boolean flag = hospitalSetService.updateById(hospitalSet);
        return result(flag);
    }

    private Result result(boolean flag) {
        if (flag)
            return Result.ok();
        else
            return Result.fail();
    }


    @PutMapping("lockHospitalSet/{id}/{status}")
    public Result lockHospitalSet(@PathVariable Long id,
                                 @PathVariable Integer status) {
        HospitalSet hospitalSet = hospitalSetService.getById(id);
        hospitalSet.setStatus(status);
        boolean flag = hospitalSetService.updateById(hospitalSet);
        return result(flag);
    }


    @PutMapping("sendKey/{id}")
    public Result sendKey(@PathVariable Long id) {
        HospitalSet hospitalSet = hospitalSetService.getById(id);
        //TODO GDL        发送 key
        hospitalSet.getSignKey();//调用其它接口 发送出
        return Result.ok();
    }


    @GetMapping("test")
    public Result test() {
        try {
            int a = 1 / 0;
        } catch (Exception exception) {
            throw new HospitalException(ResultCodeEnum.URL_ENCODE_ERROR);
        }
        return Result.ok();
    }




    @PostMapping("login")
    public Result login( ) {

        return Result.ok("admin-token");
    }
    @GetMapping("info")
    public Result info( ) {

        Map<String,Object> map=new HashMap<>();

        List<String> list=new ArrayList<>();
        list.add("admin");
        map.put("roles",list);

        map.put("introduction","I am a super administrator");
        map.put("avatar","https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif");
        map.put("name","Super Admin");

        return Result.ok(map);
    }
}
